/*****************************************************************************
* file  : systask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/
#define _GUI_MAIN_MODULE_
#include "os_obj.h"
#include "pincfg.h"
#include "dataproc.h"
#include "gui_main.h"
#include "string.h"
#include "lcd_yl.h"
#include "bsp_api.h"
#include "str_ext.h"
#include "common.h"

void img_check(void);
void spim_sw_spi_init(void);

int oled_init(void);
void gui_dbg(void);
void gui_task(void const *argument);

/******************************************************************************
 * @brief   gui task creat.
 * @param   argv
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
osThreadDef(gui_task, osPriorityNormal, 1, 0x400);
void gui_task_creat(void)
{
	os_obj.tid_gui_task = osThreadCreate(osThread(gui_task), NULL);
}
obj_init_order5(gui_task_creat);


/*****************************************************************************
* @brief   storage task.
* @param   none
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
void gui_task(void const *argument)
{
	uint16_t row_start, row_size;

	osDelay(ms_ticks(20));

	lcd_yl_init();

	pulse_init(&gui_st.pulse, app_tick_get());

	lcd_yl_origin_set(0, 16);

	gui_st.move_site_x = 0;
	lcd_yl_font_set(32, 32, Font_16_32);
	
	lcd_yl_ram_fill(0xFF);
	lcd_yl_anti();
	
	while(1)
	{
		lcd_yl_origin_set(32, 16);
		pulse_creat(&gui_st.pulse, app_tick_get());
		osSignalWait(0, ms_ticks(2));
		
		
		lcd_yl_poll(32, 2, 8);
		lcd_yl_space_set(4);
		lcd_yl_half_en();
		
		
		lcd_yl_font_set(24, 24, Font_12_24);
		lcd_yl_alain_puts(64,32,"OLED", TEXT_X_MID | TEXT_Y_MID | TEXT_SQ(2,7,0) | TEXT_LINE(2,0));
		lcd_yl_origin_set(32+128, 16);

		if(gui_st.move_mode)
		{
			break;
		}
		break;
	}

	lcd_yl_ram_fill(0);
	lcd_yl_space_set(1);
	lcd_yl_origin_set(32, 16);
	gui_st.move_site_x = 32;
	gui_st.move_site_y = 16;
	
	while (1)
	{
		if (gui_st.move_en /*&& (lcd_yl_update_cnt_get() == 7)*/)
		{
			static uint16_t mvcnt;

			mvcnt++;
			if (gui_st.move_mode)
			{
				gui_st.move_site_x++;
			}
			else
			{
				gui_st.move_site_x--;
			}

			if (gui_st.move_site_x < 0)
				gui_st.move_site_x = 0;

			if (gui_st.move_site_x > 128)
				gui_st.move_site_x = 128;

			if (mvcnt & 0x20)
				gui_st.move_site_x = mvcnt & 0x1F;
			else
				gui_st.move_site_x = 0x1F - (mvcnt & 0x1F);
			
			if (gui_st.move_site_y < 0)
				gui_st.move_site_y = 0;

			if (mvcnt & 0x40)
				gui_st.move_site_y = mvcnt & 0x3F;
			else
				gui_st.move_site_y = 0x3F - (mvcnt & 0x3F);
			
			if(gui_st.notice_mode)
			{
				gui_st.move_site_x = 32;
				gui_st.move_site_y = 16;
			}
		}

		pulse_creat(&gui_st.pulse, app_tick_get());
		
		osSignalWait(0, ms_ticks(25));

		lcd_yl_poll_ext(gui_st.move_site_x, gui_st.move_site_y, 8);		
		while(lcd_yl_update_cnt_get())
		{
			lcd_yl_poll_ext(gui_st.move_site_x, gui_st.move_site_y, 8);		
		}
		
		lcd_yl_normal();
		lcd_yl_square_fill(0,0,127,63);
		lcd_yl_anti();
		
		if(gui_st.notice_mode)
		{
			char buff[32];
			ii16 index = 0;

			lcd_yl_font_set(16, 16, Font_8_16);
			for(int cnt = 0; cnt<4; cnt++)
			{
				str_get_line((void *)&gui_st.notice_str[index], buff);

				lcd_yl_alain_puts(64, cnt * 16, buff, TEXT_X_MID);
				index = str_site_next_line((void *)&gui_st.notice_str, index);
				if(index < 0)
					break;
			}

		}
		else
		{
			lcd_yl_normal();
			lcd_yl_square_fill(0,0,127,63);
			lcd_yl_anti();
			lcd_yl_font_set(32, 32, Font_16_32);
			lcd_yl_alain_puts(64,32,"CHINA", TEXT_X_MID | TEXT_Y_MID | TEXT_SQ(2,6,0) | TEXT_LINE(1,0));
		}

		if (gui_st.page_reg >= 0)
		{
			gui_st.page_now = gui_st.page_reg;
			gui_st.page_reg = -1;
		}

		if (gui_st.pulse.pulse_100ms)
		{
			gui_st.page_keep_time_cnt++;
		}

		if (gui_st.page_old != gui_st.page_now)
		{
			gui_st.page_keep_time_cnt = 0;
		}

		gui_st.page_old = gui_st.page_now;
		gui_st.gui_cnt++;
	}
}

void gui_notice(const char *str)
{
	gui_st.notice_mode = 1;
	copy_str(str, gui_st.notice_str, 128);
}
